import { WeaTools } from 'ecCom';

window.openSPA4SingleTab = function(routeUrl,id) {
    window.openSPA4Single(routeUrl,id,true);
}

window.openSPA4Single = function(routeUrl,id,opentab) {
    const timestamp = new Date().getTime();
    const spaUrl = "/spa/workflow/static4form/index.html?_rdm="+timestamp;
    const preLoadReqInfo = routeUrl =>{
        let url = "/api/workflow/reqform/loadForm?ispreload=1&";
        url += routeUrl.split("?")[1];
        jQuery.ajax({
            type : "POST",
            url : url
        });
    }
    routeUrl += `&preloadkey=${timestamp}&timestamp=${timestamp}`;
    preLoadReqInfo(routeUrl);

    const width = screen.availWidth - 10;
    const height = screen.availHeight - 50;
    let szFeatures = "";
    if(!opentab){
        szFeatures = "top=0,left=0,";
        szFeatures += "width=" + width + ",";
        szFeatures += "height=" + height + ",";
        szFeatures += "directories=no,status=yes,toolbar=no,location=no,";
        szFeatures += "menubar=no,scrollbars=yes,resizable=yes";
    }
    window.open(spaUrl+"#"+routeUrl, "", szFeatures);
}

window.doPrintRequest = function(requestid, beluserid, otherpara=''){
    const printurl = "/workflow/request/PrintRequestForward.jsp?";
    const urlParams = `requestid=${requestid}&f_weaver_belongto_userid=${beluserid}&f_weaver_belongto_usertype=0${otherpara}`;
    openFullWindowHaveBar(printurl+urlParams);
}

window.doReadRequest = function(requestid,userid = '') {
    WeaTools.callApi('/api/workflow/reqlist/doReadIt', 'POST', { opertype: 'single', requestid,f_weaver_belongto_userid:userid,f_weaver_belongto_usertype:'0' }).then(() => {
        window._table.reLoad();	// 刷新列表
    });
};

window.showallreceived = function(requestid, returntdid){
    jQuery.ajax({
        url : "/workflow/search/WorkflowUnoperatorPersons.jsp?requestid="+ requestid + "&returntdid=" + returntdid,
        type : "post",
        success : function(res) {
            try {
                jQuery("#" + returntdid).html(jQuery.trim(res));
                jQuery("#" + returntdid).parent().attr("title", jQuery.trim(res).replace(/[\r\n]/gm, ""))
            } catch (e) {}
        }
    });
}
window.getDoing = (requestid, returntdid)=>{
    jQuery.ajax({
        url : "/api/odoc/odocNewTodoList/getOperatorPersons?requestid="+ requestid + "&persontype=1",
        type : "post",
        dataType:'json',
        success : function(res) {
            try {
                jQuery("#" + returntdid).html(res.info);
                jQuery("#" + returntdid).parent().attr("title", res.info)
            } catch (e) {}
        }
    });
}
window.getNoDo = (requestid, returntdid)=>{
    jQuery.ajax({
        url : "/api/odoc/odocNewTodoList/getOperatorPersons?requestid="+ requestid + "&persontype=0",
        type : "post",
        dataType:'json',
        success : function(res) {
            try {
                jQuery("#" + returntdid).html(res.info);
                jQuery("#" + returntdid).parent().attr("title", res.info)
            } catch (e) {}
        }
    });
}
/**
 * 默认显示未操作者设置开启，未操作者超过7个时展开、恢复数据事件
 * @param requestid
 * @param realuserid
 * @constructor
 */
window.ShowAllUnopertors = function(requestid, realuserid = "", fromWhere=""){
    var unoperatorsShort = jQuery("#"+realuserid+""+requestid+fromWhere+"span").html();
    var unoperatorsFull = jQuery("#"+realuserid+""+requestid+fromWhere+"spanAll").html();
    var displayStypeButton = jQuery("#"+realuserid+""+requestid+fromWhere+"button").css("display");
    var displayStypeButtonAll = jQuery("#"+realuserid+""+requestid+fromWhere+"buttonAll").css("display");
    if(unoperatorsShort.length > 0 && unoperatorsFull.length == 0) {//简称有值，全称没有值才走接口，否则直接调换span的值
        WeaTools.callApi('/api/workflow/reqlist/getUnoperators', 
        'POST', 
        {
          requestid:requestid, 
          realuserid:realuserid,
          actiontype:'single',
          showAll:'true',
          moduleId: fromWhere
        }).then((unopt)=>{
            jQuery("#"+realuserid+""+requestid+fromWhere+"span").html(unopt[(realuserid+""+requestid+ fromWhere+ "loding")]);
            jQuery("#"+realuserid+""+requestid+fromWhere+"spanAll").html(unoperatorsShort);
            jQuery("#"+realuserid+""+requestid+fromWhere+"button").css("display",displayStypeButtonAll);
            jQuery("#"+realuserid+""+requestid+fromWhere+"buttonAll").css("display",displayStypeButton);
        });
    }else{
        jQuery("#"+realuserid+""+requestid+fromWhere+"span").html(unoperatorsFull);
        jQuery("#"+realuserid+""+requestid+fromWhere+"spanAll").html(unoperatorsShort);
        jQuery("#"+realuserid+""+requestid+fromWhere+"button").css("display",displayStypeButtonAll);
        jQuery("#"+realuserid+""+requestid+fromWhere+"buttonAll").css("display",displayStypeButton);
    }
}

/**
 * 默认显示未操作者设置关闭，【显示】按钮事件
 * @param requestid
 * @param realuserid
 * @constructor
 */
window.ShowAllUnopertors2 = function(requestid, realuserid = "", fromWhere=''){
    WeaTools.callApi(
      '/api/workflow/reqlist/getUnoperators', 
      'POST', 
      {
        requestid:requestid, 
        realuserid:realuserid,
        actiontype:'single',
        showAll:'false',
        moduleId: fromWhere
    }).then((unopt)=>{
        jQuery("#"+realuserid+""+requestid+fromWhere + "loding").html(unopt[(realuserid+""+requestid+fromWhere+"loding")]);
    });
}

window.openFullWindowHaveBarForWFList = function(url, requestid){
    try {
        document.getElementById("wflist_" + requestid + "span").innerHTML = "";
    } catch (e) {}
    openFullWindowHaveBar(url);
}

window.openFullWindowHaveBar = function(url){
    const width = screen.availWidth - 10;
    const height = screen.availHeight - 50;
    let szFeatures = "top=0,left=0,";
    szFeatures += "width=" + width + ",";
    szFeatures += "height=" + height + ",";
    szFeatures += "directories=no,status=yes,toolbar=no,location=no,";
    szFeatures += "menubar=no,scrollbars=yes,resizable=yes";
    window.open(url, "", szFeatures);
}